//
//  BulkOrderCell.m
//  YYZB
//
//  Created by fengmengying on 2017/9/25.
//
//

#import "BulkOrderCell.h"
#import "UIView+Extension.h"
#import "ThemeColor.h"


#define ScreenWidth  [UIScreen mainScreen].bounds.size.width


@interface BulkOrderCell ()
{

    UILabel  *controlTopLabel;
    UILabel  *controlBottomLabel;


}

@end

@implementation BulkOrderCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{


    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
        
        //单品数量
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
        label.text = @"单品数量";
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = [UIColor colorWithHex:@"#858585"];
        [self.contentView addSubview:label];
        
        
        self.numLabel = [[UILabel alloc]initWithFrame:CGRectMake(label.x, label.bottom+10, 50, 20)];
        _numLabel.text = @"1台";
        _numLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_numLabel];
        
        self.priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(_numLabel.right, label.bottom+10, 50, 20)];
        _priceLabel.text = @"¥130";
        _priceLabel.font = [UIFont systemFontOfSize:14];
        _priceLabel.textColor = [ThemeColor orangeColor];
        [self.contentView addSubview:_priceLabel];
        
        
        self.detailLabel = [[UILabel alloc]initWithFrame:CGRectMake(label.x, _numLabel.bottom+10, 200, 20)];
        _detailLabel.text = @"团购以优惠¥6";
        _detailLabel.font = [UIFont systemFontOfSize:12];
        _detailLabel.textColor = [UIColor colorWithHex:@"#858585"];
        [self.contentView addSubview:_detailLabel];
        
        
        self.memoLabel = [[UILabel alloc]initWithFrame:CGRectMake(_priceLabel.right + 10, _priceLabel.y, 150, 20)];
        _memoLabel.text = @"(单品均价¥65)";
        _memoLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:_memoLabel];
        
        
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame = CGRectMake(ScreenWidth - 120, 25, 100, 50);
        [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _button.titleLabel.font = [UIFont systemFontOfSize:14];
        [_button setTitle:@"立即购买" forState:UIControlStateNormal];
        _button.backgroundColor = [ThemeColor orangeColor];
        [_button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_button];
//        _button.hidden = YES;
    
        
        
        self.control = [[UIControl alloc]initWithFrame:CGRectMake(ScreenWidth - 120, 25, 100, 50)];
        _control.layer.borderColor = [ThemeColor orangeColor].CGColor;
        _control.layer.borderWidth = 1;
        [_control addTarget:self action:@selector(controlClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_control];
        
        controlTopLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, _control.width, _control.height/2)];
        controlTopLabel.text = @"立即购买";
        controlTopLabel.font = Font(14);
        controlTopLabel.textColor = [UIColor whiteColor];
        controlTopLabel.backgroundColor = [ThemeColor orangeColor];
        controlTopLabel.textAlignment = NSTextAlignmentCenter;
        [_control addSubview:controlTopLabel];
        
        controlBottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, controlTopLabel.bottom, _control.width, _control.height/2)];
        controlBottomLabel.text = @"剩余5";
        controlBottomLabel.font = Font(14);
        controlBottomLabel.textColor = [ThemeColor orangeColor];
        controlBottomLabel.backgroundColor = [UIColor whiteColor];
        controlBottomLabel.textAlignment = NSTextAlignmentCenter;
        [_control addSubview:controlBottomLabel];
        _control.hidden = YES;

        
        
        
        
        _iconImage =[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth-30, 0, 30, 30)];
        _iconImage.image = [UIImage imageNamed:@"icon_recommend"];
        [self.contentView addSubview:_iconImage];
        
    
        
        
        
        
    }
    
    return self;


}



-(void)fillData:(GroupProject *)model{

    
    
    _iconImage.image = [UIImage imageNamed:@"icon_recommend"];
    self.numLabel.text = [NSString stringWithFormat:@"%@%@",model.productAmount,model.unit];
    _iconImage.hidden = model.isTop == 1 ? NO : YES;

    if (model.isGroupBuy == 1) { //团购产品
        self.memoLabel.hidden = NO;
        self.priceLabel.text = [NSString stringWithFormat:@"¥%.2f",[model.price floatValue]];
        self.memoLabel.text =[NSString stringWithFormat:@"(单品均价¥%.2f)",[model.averageFee floatValue]] ;
        self.detailLabel.text = [NSString stringWithFormat:@"团购已优惠¥%.2f元",[model.groupFee floatValue]];
        
        
        
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:self.detailLabel.text];
        NSRange rang = [self.detailLabel.text rangeOfString:@"¥"];
        [attributedStr addAttribute:NSForegroundColorAttributeName
                                       value:[ThemeColor orangeColor]
                              range:NSMakeRange(rang.location, self.detailLabel.text.length-rang.location)];
        self.detailLabel.attributedText = attributedStr;
        

        
        //剩余数量
        if (model.remainAmount == 0) {
            
            _button.hidden = NO;
            _control.hidden = YES;
            _button.enabled = NO;
            _control.enabled = NO;
            [_button setTitle:@"已售完" forState:UIControlStateNormal];
            _button.backgroundColor = [UIColor lightGrayColor];
            _iconImage.image = [UIImage imageNamed:@"icon_unrecommend"];

            
        }else if (model.remainAmount <= 10){
            
            _button.hidden = YES;
            _control.hidden = NO;
            controlTopLabel.text = @"立即购买";
            controlBottomLabel.text =[NSString stringWithFormat:@"剩余%ld",model.remainAmount];
            
        }else{
            
            _button.hidden = NO;
            _control.hidden = YES;
            [_button setTitle:@"立即购买" forState:UIControlStateNormal];
            _button.backgroundColor = [ThemeColor orangeColor];
            _button.enabled = YES;
        }

        
        
    }else{  //单品      团购产品对应的按钮是立即购买，单品对应的按钮是立即预约
    
        self.memoLabel.hidden = YES;
        self.priceLabel.text = [NSString stringWithFormat:@"¥%.2f起",[model.price floatValue]];
        self.detailLabel.text = [NSString stringWithFormat:@"最终以帮工上门实际服务金额为准"];
        
        _button.hidden = NO;
        _control.hidden = YES;
        [_button setTitle:@"立即预约" forState:UIControlStateNormal];
        _button.backgroundColor = [ThemeColor orangeColor];
        _button.enabled = YES;

    }
 
    CGSize size = [_priceLabel.text sizeWithFont:Font(14) maxSize:CGSizeMake(100, 20)];
    _priceLabel.width = size.width+20;


}

-(void)buttonClick:(UIButton *)sender{

    if (self.detailBlock) {
        self.detailBlock(sender.titleLabel.text, sender.tag);
    }

}

-(void)controlClick:(UIControl *)sender{


    if (self.detailControlBlock) {
        self.detailControlBlock(controlTopLabel.text,sender.tag);
    }
}


+(CGFloat)cellHeight{

    return 100;

}
@end
